﻿using System.Collections.Generic;
using Quartz;

namespace SkypeBot.Core.Scheduler.Jobs
{
    using SKYPE4COMLib;

    public class DailySpammerDeterminationJob : IJob
    {
        List<Spammer> objSpammersList = new List<Spammer>();

        public void Execute(IJobExecutionContext context)
        {
            string strResponce = string.Empty;
            foreach(var receiver in Core.Instance.Receivers)
            {
                if(receiver.IsChat)
                {
                    foreach (Chat chat in Core.Instance.Chats)
                    {
                        string str = chat.Name;
                        if (chat.Name == receiver.ReceiverName)
                        {
                            foreach (User user in chat.Members)
                            {
                                objSpammersList.Add(new Spammer(user.Handle, 0, 0));
                            }

                            foreach (Message objMessage in Core.Instance.Messages)
                            {
                               if(objMessage.ChatName == receiver.ReceiverName)
                               {
                                   foreach(Spammer spammer in objSpammersList)
                                   {
                                       if (objMessage.SenderName == spammer.User)
                                       {
                                           spammer.CountMessages++;
                                           spammer.CountSymbols = objMessage.MessageContent.Replace(" ", string.Empty).ToCharArray().Length;
                                       }
                                   }
                               }
                            }

                            int CountAllMessages = 0;
                            int CountAllSymbols = 0;
                            strResponce = "По итогам сегодняшнего дня:\r\n";
                            foreach(Spammer spammer in objSpammersList)
                            {
                               CountAllMessages += spammer.CountMessages;
                               CountAllSymbols+= spammer.CountSymbols;
                            }

                            objSpammersList.Sort(new SymbolCompareSpam<Spammer>());
                            strResponce += string.Format("В номинации \"Пианист дня\" побеждает - {0} с общим количеством символов {1} ({2}%),\r\n", objSpammersList[0].User, objSpammersList[0].CountSymbols, (double)((double)(100.0 / CountAllSymbols)) * objSpammersList[0].CountSymbols);
                            for(int i=1; i<objSpammersList.Count;i++)
                            {
                                strResponce += string.Format("{0}-е место занимает - {1} с общим количеством символов {2} ({3}%),\r\n", i+1, objSpammersList[i].User, objSpammersList[i].CountSymbols, (double)((double)(100.0/CountAllSymbols))*objSpammersList[i].CountSymbols);
                            }

                            strResponce += "Общее количество символов:" + CountAllSymbols + "\r\n";
                            objSpammersList.Sort(new MessageCompareSpam<Spammer>());
                            strResponce += string.Format("В номинации \"Есть что сказать - валяй\" побеждает - {0} с общим количеством сообщений {1} ({2}%),\r\n", objSpammersList[0].User, objSpammersList[0].CountMessages, CountAllMessages != 0 ? (double)((double)(100.0 / CountAllMessages)) * objSpammersList[0].CountMessages : 0);
                            for(int i=1; i<objSpammersList.Count;i++)
                            {
                                strResponce += string.Format("{0}-е место занимает - {1} с общим количеством сообщений {2} ({3}%),\r\n", i + 1, objSpammersList[i].User, objSpammersList[i].CountMessages, CountAllMessages != 0 ? (double)((double)(100.0 / CountAllMessages)) * objSpammersList[i].CountMessages : 0);
                            }

                            strResponce += "Общее количество сообщений:" + CountAllMessages + "\r\n";
                            Core.Instance.SendToRecievers(new Responce(receiver, strResponce));
                            objSpammersList.Clear();
                        }
                    }
                    
                }

            }

            Core.Instance.Messages.Clear();
        }
    }

    public class Spammer
    {
        public Spammer(string strUserName, int intCountMessages, int intCountSymbols)
        {
            User = strUserName;
            CountMessages = intCountMessages;
            CountSymbols = intCountSymbols;
        }

        public string User { get; set; }
        public int CountMessages { get; set; }
        public int CountSymbols { get; set; }

    }

    class SymbolCompareSpam<T> : IComparer<T> 
        where T : Spammer
    {

        public int Compare(T x, T y)
        {
            if (x.CountSymbols < y.CountSymbols)
                return 1;
            if (x.CountSymbols > y.CountSymbols)
                return -1;
            else return 0;
            
        }
    }

    class MessageCompareSpam<T> : IComparer<T>
        where T : Spammer
    {

        public int Compare(T x, T y)
        {
            if (x.CountMessages < y.CountMessages)
                return 1;
            if (x.CountMessages > y.CountMessages)
                return -1;
            else return 0;

        }
    }
}
